//
//  XSJNetWorkTools.swift
//  weiBo项目准备
//
//  Created by 鲜世杰 on 16/5/12.
//  Copyright © 2016年 鲜世杰. All rights reserved.
//

import UIKit
import AFNetworking

enum ReqeustType : Int{
    case GET = 0
    case POST = 1
}

class XSJNetWorkTools: AFHTTPSessionManager {

    static let sharedTools: XSJNetWorkTools = {
        let tools = XSJNetWorkTools()
        //  设置可以接受的响应类型
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()

    
    func requestUpload(URL : String , params : AnyObject? , data : NSData , name : String , callBack: (response: AnyObject?, error: NSError?)->()) {
    
        POST(URL, parameters: params, constructingBodyWithBlock: { (formData) -> Void in
            
                        formData.appendPartWithFileData(data, name: "pic", fileName: "test", mimeType: "image/jpeg")
            
                        }, progress: nil, success: { (_, response) -> Void in
                            callBack(response: response, error: nil)
            
                        }) { (_, error) -> Void in
                            callBack(response: nil, error: error)
                    }
        
    }
    
    
    
    func request(requestType: ReqeustType, url: String, params: AnyObject?, callBack: (response: AnyObject?, error: NSError?)->()) {
        //  定义一个成功的闭包
        let successClosure = { (dataTask: NSURLSessionDataTask, response: AnyObject?) -> Void in
            //  成功的回调
            callBack(response: response, error: nil)
        }
        
        let failureClosure = { (dataTask: NSURLSessionDataTask?, error: NSError) -> Void in
            //  失败的回调
            callBack(response: nil, error: error)
            
        }
        
        
        if requestType == .GET {
            GET(url, parameters: params, progress: nil, success: successClosure, failure: failureClosure)
            
        } else {
            POST(url, parameters: params, progress: nil, success: successClosure, failure: failureClosure)
        }
    }
    
}


extension XSJNetWorkTools {

    func upload(access_token : String, status : String ,image : UIImage , callBack: (response: AnyObject?, error: NSError?)->()){
    
        let url = "https://upload.api.weibo.com/2/statuses/upload.json"
        let params = [
            
            "access_token": access_token,
            "status": status
        ]
        let data = UIImageJPEGRepresentation(image, 0.6)
        
        requestUpload(url, params: params, data: data!, name: "pic", callBack: callBack)
        
//        POST(url, parameters: params, constructingBodyWithBlock: { (formData) -> Void in
//            
//            formData.appendPartWithFileData(data!, name: "pic", fileName: "test", mimeType: "image/jpeg")
//            
//            }, progress: nil, success: { (_, response) -> Void in
//                callBack(response: response, error: nil)
//
//            }) { (_, error) -> Void in
//                callBack(response: nil, error: error)
//        }
        
    }
    
    func update(access_token: String, status: String, callBack: (response: AnyObject?, error: NSError?)->()) {
        let url = "https://api.weibo.com/2/statuses/update.json"
        let params = [
            "access_token": access_token,
            "status": status
        ]
        
        
        request(.POST, url: url, params: params, callBack: callBack)
        
    }
    
}


extension XSJNetWorkTools {
    //  请求微博首页数据
    func requestStatus(accessToken: String, maxId : Int64 = 0, sinceId: Int64 = 0 , callBack: (response: AnyObject?, error: NSError?)->()) {
        //  创建url
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        //  设置参数
        let params = [
            "access_token": accessToken,
            "max_id" : "\(maxId)",
            "since_id": "\(sinceId)"

        ]
//        print("haha-" + url + "?access_token=\(accessToken)")
        
        request(.GET, url: url, params: params, callBack: callBack)
        
    }
}


extension XSJNetWorkTools {
    //  获取accesstoken的接口
    func requestAccessToken(code: String, callBack: (response: AnyObject?, error: NSError?)->()) {
        //  创建url
        let url = "https://api.weibo.com/oauth2/access_token"
        //  设置参数
        let params = [
            "client_id": WeiboAppKey,
            "client_secret": WeiboAppSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": WeiboRedirect_Uri
        ]
        
        request(.POST, url: url, params: params, callBack: callBack)
    }
    
    //  获取用户信息
    func requestUserInfo(userAccount: XSJUserAocut, callBack: (response: AnyObject?, error: NSError?)->()) {
        //  url
        let url = "https://api.weibo.com/2/users/show.json"
        //  设置参数
        let params = [
            "access_token": userAccount.access_token!,
            "uid": "\(userAccount.uid)"
        ]
        request(.GET, url: url, params: params, callBack: callBack)
        
        
    }
    
}
